<?php

class Item extends Zend_Db_Table_Abstract
{
    protected $_name = 'cms_order_item';
    protected $_primary = 'item_id';
    
    public function getItemsByOrderId($orderId, $page = 1)
    {
        $tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $tb->select()
        			->from(array('o' => 'cms_order'))
       				->join(array('i' => 'cms_order_item'), 'o.order_id = i.order_id')
					->join(array('d' => 'cms_deal'), 'd.deal_id = i.deal_id')
                    ->where("o.order_id = ?", $orderId);
        $paginator = Zend_Paginator::factory($select);
    	$paginator->setItemCountPerPage(Zend_Registry::get('page_size'));
    	$paginator->setCurrentPageNumber($page); 
    	
        return $paginator;
    }
    
    public function getItemsByDealId($deal_id, $page = 1, $order = "o.created desc")
    {
        $tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $tb->select()
        			->from(array('o' => 'cms_order'))
       				->join(array('i' => 'cms_order_item'), 'o.order_id = i.order_id')
       				->join(array('d' => 'cms_deal'), 'd.deal_id = i.deal_id')
       				->join(array('u' => 'user_account'), 'u.user_id = o.user_id')
                    ->where("d.deal_id = ?", $deal_id)
                    ->order($order);
        $paginator = Zend_Paginator::factory($select);
    	$paginator->setItemCountPerPage(Zend_Registry::get('page_size'));
    	$paginator->setCurrentPageNumber($page); 
    	
        return $paginator;
    }
    
    public function deleteItemsByOrderId($orderId)
    {
    	$query = "delete from cms_order_item where deal_id = $orderId";
        $tb = Zend_Db_Table::getDefaultAdapter();
        $tb->setFetchMode(Zend_Db::FETCH_OBJ);
        $tb->fetchAll($query);
    }
    
    public function getItemById($id){
        $row = $this->fetchRow($this->select()->where('item_id=?', $id));
        
        return $row;
    }
}